package myJs

import myJs.myPkg.bootstrap.Bootstrap.default._
import myJs.Tool._
import myJs.Utils._
import myJs.Implicits._
import myJs.myPkg.{Swal, SwalOptions}
import myJs.myPkg.jquery._
import scalatags.Text.all._

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.scalajs.js.Dynamic.{global=>g}

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("DetailInfo")
object DetailInfo {

  @JSExport("init")
  def init = {

  }

  @JSExport("updateBefore")
  def updateBefore = {
    val formId = "updateForm"
    val url = g.jsRoutes.controllers.UserController.getUserById().url.toString
    val ajaxSettings = JQueryAjaxSettings.url(s"${url}").
      `type`("get").success { (data, status, e) =>
      val rs = data.asInstanceOf[js.Dictionary[String]]
      fillByNames(rs, formId)
      jQuery("#updateModal").modal("show")
    }
    $.ajax(ajaxSettings)
  }

  @JSExport("update")
  def update = {
    val formId = "updateForm"
    val data = $(s"#${formId}").serialize()
    val index = layer.alert(myElement, layerOptions)
    val url = g.jsRoutes.controllers.UserController.updateInfo().url.toString
    val ajaxSettings = JQueryAjaxSettings.url(url).`type`("post").data(data).success { (data, status, e) =>
      refreshUser{()=>
        layer.close(index)
        jQuery("#updateModal").modal("hide")
        Swal.swal(SwalOptions.title("成功").text("修改成功!").`type`("success"))
      }

    }
    $.ajax(ajaxSettings)
  }

  @JSExport("refreshUser")
  def refreshUser(f: () => js.Any = () => ()) = {
    val url = g.jsRoutes.controllers.UserController.getUserById().url.toString
    val ajaxSettings = JQueryAjaxSettings.url(s"${url}?").contentType("application/json").
      `type`("get").success { (data, status, e) =>
      val rs = data.asInstanceOf[js.Dictionary[String]]
      Tool.fillByIds(rs)
      f()
    }
    $.ajax(ajaxSettings)

  }


}
